package com.twotiger.and.activity.account.cash;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.account.capitalflow.CapitalFlowPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.BanklistItem;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.BroswerUrl;
import com.twotiger.and.bean.CashBank;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.bean.UserAsset;
import com.twotiger.and.bean.Withdraw;
import com.twotiger.and.bean.WithdrawBank;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.DownPicUtil;
import com.twotiger.and.util.KeyboradListener;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.LogUtil;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ReChargeClickListener;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.ViewUtils;

/**
 * 
 * @Description: 
 *  取现 
 * @Author douxy
 * @Date 2015年8月12日 下午1:59:08
 */
public class CashPage extends BaseActivity implements OnClickListener {

    private static final int BINDCARDSUCCCES = 0;
    private static final int BINDCARDFAIL = 1;
    private static final int GETCASHSUCCES = 2;
    private static final int GETCASHFAIL = 3;
    private static final int GETBANKSUCCES = 4;
    private static final int GETBANKFAIL = 5;
    private static final int REQUESTCODE = 6;
    private static final int WITHDRAWSUCCES = 7;
    private static final int WITHDRAWFAIL = 8;
    private static final int GETUSERASSERTSUCCES = 9;
    private static final int GETUSERASSERTFAIL = 10;

    /**绑卡流程各个页面启动标志*/
    public static final int BINGCARDREQUEST = 14;
    /**绑卡流程各个页面关闭的标示(绑卡成功)*/
    public static final int BINGCARDRESULTCLOSE_SUC = 15;

    /**取现流程各个页面启动标志*/
    public static final int CASHREQUEST = 11;
    /**取现流程各个页面关闭的标示(取现成功)*/
    public static final int CASHRESULTCLOSE_SUC = 12;
    /**取现流程各个页面关闭的标示(取现失败)*/
    public static final int CASHRESULTCLOSE_FAI = 13;

    private static final int LOOPTOCOUNTINSERT = 16;

    private EditText cash_amount;
    private TextView money_avaliable;
    private Button conmmit_btn;
    private HashMap<String, String> params;
    private ImageView bankcard_icon;
    private TextView bankcard_no;
    private TextView bank_detail_text;
    private View bankcard_layout;
    private List<BanklistItem> banklist;

    private SubTitle subTitle;
    private ImageView more_bankcard;
    private CashBank cashBankCard;
    private Withdraw withDraw;
    private UserAsset userAsset;

    public static final String BINDCARD = "bindcard";

    public static final String CHECKBYQUICKBANK = "quickBank"; //绑卡成功与否 的判断规则为：快捷卡是否为可用
    public static final String CHECKBYNOTQUICKBANK = "not_quickBank"; //绑卡成功与否 的判断规则为：用户银行卡数量的增加与否

    public static final String CASHTYPETIMELY = "immediate";
    public static final String CASHTYPECOMMON = "general";

    public String check_tag; //绑卡成功与否 的判断规则（传到webviewpage 让其判断！！）
    private WithdrawBank mWithdrawBank;
    private TextView tv_unbind_bank;
    private RelativeLayout cash_layout_timely, cash_layout_common;
    private CheckBox cb_cash_timely, cb_cash_common;
    private TextView tv_cash_timely_des, tv_cash_common_des;
    private LinearLayout ll_mianfei;
    private LinearLayout main;
    private TextView tv_count, tv_yuan;
    private TextView tv_charge_fee;
    private String amountContent; //金额输入框的内容
    private String tempNumNow;

    private boolean canClickBtn = true; //提现按钮是否可以点击
    private boolean isAskWithDrawFee; //是否需要请求手续费   false表示还有免费次数，true表示无免费次数
    private boolean isClickAllBtn; //是否点击了全提按钮
    private boolean isClickAllBtn222; //是否响应edittext内容变化
    private boolean isChangeClickAllBtnState = true; // 是否改变是否全提的状态

    private String cashType;
    private Button all_inverst_btn;
    private TextView tv_cash_tip;
    private ImageView iv_cash_timely;
    private TextView tv_cash_timely, tv_cash_common;
    private ImageView iv_cash_common;

    /**当日提现相关信息*/

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.cash_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        main = (LinearLayout) view.findViewById(R.id.main);
        conmmit_btn = (Button) view.findViewById(R.id.conmmit_btn);
        bankcard_icon = (ImageView) view.findViewById(R.id.bankcard_quicon);
        bankcard_layout = view.findViewById(R.id.bankcard_layout_qu);
        bankcard_no = (TextView) view.findViewById(R.id.bankcard_quno);
        bank_detail_text = (TextView) view.findViewById(R.id.bank_qudetail_text);
        cash_amount = (EditText) view.findViewById(R.id.cash_amount);
        money_avaliable = (TextView) view.findViewById(R.id.money_avaliable);
        more_bankcard = (ImageView) view.findViewById(R.id.more_bankcard);
        tv_unbind_bank = (TextView) view.findViewById(R.id.tv_unbind_bank);

        cash_layout_timely = (RelativeLayout) view.findViewById(R.id.cash_layout_timely);
        tv_cash_timely_des = (TextView) view.findViewById(R.id.tv_cash_timely_des);
        cb_cash_timely = (CheckBox) view.findViewById(R.id.cb_cash_timely);
        cash_layout_common = (RelativeLayout) view.findViewById(R.id.cash_layout_common);
        tv_cash_common_des = (TextView) view.findViewById(R.id.tv_cash_common_des);
        cb_cash_common = (CheckBox) view.findViewById(R.id.cb_cash_common);

        tv_charge_fee = (TextView) view.findViewById(R.id.tv_charge_fee);
        ll_mianfei = (LinearLayout) view.findViewById(R.id.ll_mianfei);
        tv_count = (TextView) view.findViewById(R.id.tv_count);
        tv_yuan = (TextView) view.findViewById(R.id.tv_yuan);
        all_inverst_btn = (Button) view.findViewById(R.id.all_inverst_btn);
        tv_cash_tip = (TextView) view.findViewById(R.id.tv_cash_tip);
        iv_cash_timely = (ImageView) view.findViewById(R.id.iv_cash_timely);
        iv_cash_common = (ImageView) view.findViewById(R.id.iv_cash_common);
        tv_cash_timely = (TextView) view.findViewById(R.id.tv_cash_timely);
        tv_cash_common = (TextView) view.findViewById(R.id.tv_cash_common);

        cash_amount.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0)
                    return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });
        subTitle = new SubTitle(view) {

            @Override
            protected void clickTitle(View v) {

            }

            @Override
            protected void clickRightBtn(View v) {
                Intent intent = new Intent(mContext, CapitalFlowPage.class);
                intent.putExtra("REFRESHTYPE", 2);
                startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("提现");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
        subTitle.title_right_tv.setText("提现记录");
        subTitle.title_right_tv.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onRestart() {
        getuserAssert();
        super.onRestart();
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
        case R.id.tv_cash_tip:
            toCashTips();
            break;
        case R.id.all_inverst_btn: //全提
            isClickAllBtn222 = true;
            if (CASHTYPETIMELY.equals(cashType)) {
                if (cashBankCard.getImmediateAllAmount() <= 0.0) {
                    return;
                }
                cash_amount
                    .setText(ArithUtils.coverMoney(cashBankCard.getImmediateAllAmount() + ""));
                cash_amount.setSelection(ArithUtils.coverMoney(
                    cashBankCard.getImmediateAllAmount() + "").length());
                if (isAskWithDrawFee) {
                    tv_charge_fee.setText(ArithUtils.coverMoney(cashBankCard.getImmediateAllFee()
                                                                + ""));
                }
            } else if (CASHTYPECOMMON.equals(cashType)) {
                if (cashBankCard.getGeneralAllAmount() <= 0.0) {
                    return;
                }
                cash_amount.setText(ArithUtils.coverMoney(cashBankCard.getGeneralAllAmount() + ""));
                cash_amount.setSelection(ArithUtils.coverMoney(
                    cashBankCard.getGeneralAllAmount() + "").length());
                if (isAskWithDrawFee) {
                    tv_charge_fee.setText(ArithUtils.coverMoney(cashBankCard.getGeneralAllFee()
                                                                + ""));
                }
            }
            isClickAllBtn222 = false;
            isClickAllBtn = true;
            break;
        case R.id.cash_layout_timely: //即时提现
            if ("0".equals(cashBankCard.getIsImmediateCash())) {
                return;
            }
            if (CASHTYPETIMELY.equals(cashType)) {
                return;
            }
            cash_layout_timely.setBackgroundColor(Color.parseColor("#fef9f3"));
            cash_layout_common.setBackgroundColor(Color.parseColor("#ffffff"));
            cb_cash_timely.setChecked(true);
            cb_cash_common.setChecked(false);
            cashType = CASHTYPETIMELY;
            if (isClickAllBtn) {
                isChangeClickAllBtnState = false;
                isClickAllBtn222 = true;
                cash_amount
                    .setText(ArithUtils.coverMoney(cashBankCard.getImmediateAllAmount() + ""));
                cash_amount.setSelection(ArithUtils.coverMoney(
                    cashBankCard.getImmediateAllAmount() + "").length());
                if (isAskWithDrawFee) {
                    tv_charge_fee.setText(ArithUtils.coverMoney(cashBankCard.getImmediateAllFee()
                                                                + ""));
                }
                isChangeClickAllBtnState = true;
                isClickAllBtn222 = false;
            }else {
                if (isAskWithDrawFee) {
                    towithdraw(cash_amount.getText().toString().trim());
                }
            }
            break;
        case R.id.cash_layout_common: //普通提现
            if ("0".equals(cashBankCard.getIsGeneralCash())) {
                return;
            }
            if (CASHTYPECOMMON.equals(cashType)) {
                return;
            }
            cash_layout_common.setBackgroundColor(Color.parseColor("#fef9f3"));
            cash_layout_timely.setBackgroundColor(Color.parseColor("#ffffff"));
            cb_cash_timely.setChecked(false);
            cb_cash_common.setChecked(true);
            cashType = CASHTYPECOMMON;
            if (isClickAllBtn) {
                isChangeClickAllBtnState = false;
                isClickAllBtn222 = true;
                cash_amount.setText(ArithUtils.coverMoney(cashBankCard.getGeneralAllAmount() + ""));
                cash_amount.setSelection(ArithUtils.coverMoney(
                    cashBankCard.getGeneralAllAmount() + "").length());
                if (isAskWithDrawFee) {
                    tv_charge_fee.setText(ArithUtils.coverMoney(cashBankCard.getGeneralAllFee()
                                                                + ""));
                }
                isChangeClickAllBtnState = true;
                isClickAllBtn222 = false;
            }else {
                if (isAskWithDrawFee) {
                    towithdraw(cash_amount.getText().toString().trim());
                }
            }
            break;
        case R.id.tv_unbind_bank:
            PromptManager.showUnbindBankAlert(CashPage.this, new ReChargeClickListener() {

                @Override
                public void onClickConfirm() {

                }

                @Override
                public void onClickCancel() {

                }
            });
            break;
        case R.id.bankcard_layout_qu:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            intent.setClass(mContext, CashBankCardList.class);
            startNewActivityforresult(intent, R.anim.push_left_in, R.anim.push_left_out,
                REQUESTCODE);
            break;
        case R.id.conmmit_btn: // 提现, 要判断余额
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (!canClickBtn) {
                return;
            }
            if ("0".equals(cashBankCard.getIsGeneralCash()) && "0".equals(cashBankCard.getIsImmediateCash())) {
                showToast("银行系统维护，暂不支持提现操作");
                return;
            }
            String withdrawMoney = cash_amount.getText().toString().trim();
            if (StringUtils.isEmpty(withdrawMoney)) {
                showToast("请输入提现金额");
                return;
            }
            if (StringUtils.isEmpty(withdrawMoney)) {
                withdrawMoney = "0.00";
            }
            if (ArithUtils.compareTo(withdrawMoney, "0.00") == 0) {
                showToast("提现金额不能为零");
                return;
            }
            if (StringUtils.isEmpty(String.valueOf(userAsset.avaliable))) {
                showToast("请确认账户有可用余额");
                return;
            }
            if (cashBankCard == null) {
                showToast("请先选择提现银行");
                return;
            }
            if (StringUtils.isEmpty(cashType)) {
                showToast("请先选择提现方式");
                return;
            }
            double tempFee = 0.0;
            if (CASHTYPETIMELY.equals(cashType)) {
                tempFee = withDraw.getImmediateFee();
            } else if (CASHTYPECOMMON.equals(cashType)) {
                tempFee = withDraw.getGeneralFee();
            }
            if (ArithUtils.compareTo(String.valueOf(ArithUtils.sub(userAsset.avaliable, tempFee)),
                withdrawMoney) == -1) {
                //                PromptManager.showDebtAlert(CashPage.this, "可用余额不足", "提现额+手续费不可大于可用余额");
                PromptManager.showConfirmAlertCommon(CashPage.this, 0, "可用余额不足",
                    "提现额+手续费不可大于可用余额", "我知道了", "", false, new ReChargeClickListener() {

                        @Override
                        public void onClickConfirm() {

                        }

                        @Override
                        public void onClickCancel() {

                        }
                    });
                return;
            }
            getcash(ArithUtils.coverMoney(withdrawMoney));
            break;

        }
    }

    /**
     * 提现接口
     * 
     * @param withdrawMoney
     */
    private void getcash(String withdrawMoney) {
        params.clear();
        params.put("token", getToken());
        params.put("amount", ArithUtils.coverMoney(withdrawMoney));
        params.put("cardNo", cashBankCard.cardNo);
        params.put("type", cashType);
        double tempFee = 0.0;
        if(isClickAllBtn){
            if (CASHTYPETIMELY.equals(cashType)) {
                tempFee = cashBankCard.getImmediateAllFee();
            } else if (CASHTYPECOMMON.equals(cashType)) {
                tempFee = cashBankCard.getGeneralAllFee();
            }
        }else{
            if (CASHTYPETIMELY.equals(cashType)) {
                tempFee = withDraw.getImmediateFee();
            } else if (CASHTYPECOMMON.equals(cashType)) {
                tempFee = withDraw.getGeneralFee();
            }
        }
        params.put("fee", ArithUtils.coverMoney(tempFee + ""));
        getDate(params, Constants.WITHDRAW, handler, GETCASHSUCCES, GETCASHFAIL, true, false, false);
    }

    /**
     * 获取用户资金信息
     */
    private void getuserAssert() {
        params.clear();
        params.put("token", getToken());
        getDate(params, Constants.ASSETQUERY, handler, GETUSERASSERTSUCCES, GETUSERASSERTFAIL,
            false, true, false);
    }

    /**
     * 预提现接口
     */
    private void towithdraw(String money) {
        if (StringUtils.isEmpty(money)) {
            return;
        }
        tv_charge_fee.setText("计算中...");
        ll_mianfei.setVisibility(View.GONE);
        tv_yuan.setVisibility(View.GONE);
        canClickBtn = false;
        params.clear();
        params.put("token", getToken());
        params.put("amount", ArithUtils.coverMoney(money));
        params.put("cardNo", cashBankCard.cardNo);
        getDate(params, Constants.TOWITHDRAW, handler, WITHDRAWSUCCES, WITHDRAWFAIL, false, false,
            false);
    }

    /**
     * 绑定银行卡
     */
    private void bindcard() {
        params.clear();
        params.put("token", getToken());
        getDate(params, Constants.BINDBANK, handler, BINDCARDSUCCCES, BINDCARDFAIL, true, true,
            false);
    }

    @Override
    protected void onActivityResult(int requestcode, int resultcode, Intent intent) {
        if (requestcode == REQUESTCODE && resultcode == RESULT_OK) {
            int idx = intent.getIntExtra("idx", 0);
            for (int i = 0; i < application.getcashBanks().size(); i++) {
                if (i == idx) {
                    application.getcashBanks().get(idx).setCashChecked(true);
                } else {
                    application.getcashBanks().get(i).setCashChecked(false);
                }
            }
            setCashBankCard(application.getcashBanks().get(idx));
        }
        if (requestcode == CASHREQUEST) { //取现流程
            if (resultcode == CASHRESULTCLOSE_SUC) {
                finish();
            }
        }
        if (requestcode == BINGCARDREQUEST) { //绑卡流程
            if (resultcode == BINGCARDRESULTCLOSE_SUC) {
                getuserbankcards();
            } else {
                getuserbankcards(); //绑卡失败回来继续显示弹窗提示
            }
        }
        super.onActivityResult(requestcode, resultcode, intent);
    }

    @Override
    protected void setListener() {

        amountContent = cash_amount.getText().toString().trim();

        tv_cash_tip.setOnClickListener(this);
        all_inverst_btn.setOnClickListener(this);
        cash_layout_timely.setOnClickListener(this);
        cash_layout_common.setOnClickListener(this);
        conmmit_btn.setOnClickListener(this);
        bankcard_layout.setOnClickListener(this);
        tv_unbind_bank.setOnClickListener(this);
        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETBANKSUCCES:
                    Basebean result_b = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_b.isOk()) {
                        mWithdrawBank = JSON.parseObject(result_b.data, WithdrawBank.class);
                        List<CashBank> cashBankList = JSON.parseArray(mWithdrawBank.getBankList(),
                            CashBank.class);
                        application.setCashBanks(cashBankList); //为了方便绑卡比较
                        initPage();
                    }
                    break;
                case GETBANKFAIL:
                    showToast("网络异常");
                    break;
                case WITHDRAWSUCCES:
                    Basebean result_draw = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    canClickBtn = true;
                    if (result_draw.isOk()) {
                        withDraw = JSON.parseObject(result_draw.data, Withdraw.class);
                        ll_mianfei.setVisibility(View.GONE);
                        tv_yuan.setVisibility(View.VISIBLE);
                        if (CASHTYPETIMELY.equals(cashType)) {
                            tv_charge_fee.setText(ArithUtils.coverMoney(withDraw.getImmediateFee()
                                                                        + ""));
                        } else if (CASHTYPECOMMON.equals(cashType)) {
                            tv_charge_fee.setText(ArithUtils.coverMoney(withDraw.getGeneralFee()
                                                                        + ""));
                        } else{
                            tv_charge_fee.setText("--");
                        }

                    } else {
                        showToast(result_draw.codeDesc);
                    }
                    break;
                case BINDCARDSUCCCES:
                    Basebean result_bind = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_bind.isOk()) {
                        BroswerUrl broswerurl = JSON.parseObject(result_bind.data, BroswerUrl.class);
                        Intent intent = new Intent(mContext, WebViewPage.class);
                        intent.putExtra("title", "绑定提现银行卡");
                        intent.putExtra("SHOWPASSWORD", "showPassword");
                        intent.putExtra("url", broswerurl.getUrl());
                        intent.putExtra("isBindCard", BINDCARD);
                        intent.putExtra("check_tag", check_tag);
                        //startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, true);
                        startNewActivityforresult(intent, R.anim.push_left_in,
                            R.anim.push_left_out, BINGCARDREQUEST);
                    } else {
                        showToast(result_bind.codeDesc);
                    }
                    break;
                case GETCASHSUCCES:
                    Basebean result_w = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result_w.isOk()) {
                        BroswerUrl url = JSON.parseObject(result_w.data, BroswerUrl.class);
                        Intent intent = new Intent(mContext, WebViewPage.class);
                        intent.putExtra("title", "提现");
                        intent.putExtra("url", url.getUrl());
                        intent.putExtra("SHOWPASSWORD", "showPassword");
                        intent.putExtra("BUSINESS_TYPE", WebViewPage.TYPE_CASE);
                        if (CASHTYPETIMELY.equals(cashType)) {
                            intent.putExtra("EXPECTTIME", cashBankCard.getImmediateCashMsg());
                        } else if (CASHTYPECOMMON.equals(cashType)) {
                            intent.putExtra("EXPECTTIME", cashBankCard.getGenneralCashMsg());
                        }
                        startNewActivityforresult(intent, R.anim.push_left_in,
                            R.anim.push_left_out, CASHREQUEST);
                    } else {
                        showToast(result_w.codeDesc);
                    }
                    break;
                case GETCASHFAIL:

                    break;
                case GETUSERASSERTSUCCES:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        userAsset = JSON.parseObject(result.data, UserAsset.class);
                        application.setUserAsset(userAsset);
                        setCashInfo();
                    } else {
                        showToast(userAsset.codeDesc);
                    }
                    break;
                case GETUSERASSERTFAIL:

                    break;
                case LOOPTOCOUNTINSERT: //轮询看是否需要计算收益
                    tempNumNow = amountContent;
                    String temp = (String) msg.obj;
                    if (!StringUtils.isEmpty(tempNumNow) && Double.parseDouble(tempNumNow) != 0) {
                        if (tempNumNow.equals(temp)) {
                            if (!ArithUtils.isValidAmount(tempNumNow)) {
                                showToast("请输入有效金额");
                                break;
                            }
                            towithdraw(tempNumNow.toString());
                        }
                    }
                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };

        cash_amount.addTextChangedListener(new TextWatcher() {

            String aaa = "";

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                aaa = s.toString(); //edittext变化之前的内容
            }

            @Override
            public void afterTextChanged(Editable s) {
                String bbb = s.toString();//edittext变化之后的内容
                if (".".equals(s.toString())) {
                    cash_amount.setText("");
                    return;
                }

                if (!bbb.equals(aaa)) {
                    if(isClickAllBtn222){
                        if (isChangeClickAllBtnState) {
                            isClickAllBtn = false;
                        }
                        return;  //多一个return，表示全提状态的时候改变edittext中内容直接返回
                    }else{
                        if (isChangeClickAllBtnState) {
                            isClickAllBtn = false;
                        }
                    }
                    
                    if (!StringUtils.isEmpty(s.toString())) {
                        if (ArithUtils.compareTo(String.valueOf(userAsset.avaliable), s.toString()) == -1) {
                            cash_amount.setText(aaa);
                            cash_amount.setSelection(aaa.length() - 1);
                            return;
                        }
                    }
                    if (!isAskWithDrawFee) {
                        return;
                    }
                    if (!StringUtils.isEmpty(s.toString())) {
                        if (Double.parseDouble(s.toString()) == 0) {
                            tv_charge_fee.setText("--");
                            ll_mianfei.setVisibility(View.GONE);
                            tv_yuan.setVisibility(View.VISIBLE);
                            amountContent = s.toString();
                        } else {
                            tv_charge_fee.setText("计算中...");
                            ll_mianfei.setVisibility(View.GONE);
                            tv_yuan.setVisibility(View.GONE);
                            amountContent = s.toString();
                        }
                    } else {
                        tv_charge_fee.setText("--");
                        ll_mianfei.setVisibility(View.GONE);
                        tv_yuan.setVisibility(View.VISIBLE);
                        amountContent = s.toString();
                    }
                    String tempNumPre = amountContent;
                    Message preNumMes = Message.obtain();
                    preNumMes.obj = tempNumPre;
                    preNumMes.what = LOOPTOCOUNTINSERT;
                    handler.sendMessageDelayed(preNumMes, 1000); //发送一个延迟3秒的消息。系统收到消息后会对比文本框内容是否有变化。如果没有变化则请求预期收益接口

                    canClickBtn = false;

                }
            }
        });
    }

    @Override
    protected void processLogic() {

        mWithdrawBank = (WithdrawBank) getIntent().getSerializableExtra("WITHDRAWBANK");

        params = getParm();
        userAsset = application.getUserAsset();
        withDraw = new Withdraw();
        withDraw.setGeneralFee(0);
        withDraw.setImmediateFee(0);
        withDraw.setToDay(DateUtil.getTomoData());
        setCashInfo();
        initPage();
        initData();
    }

    private void initData() {
        getuserAssert();

    }

    /**
     * 获取提现银行卡列表
     */
    private void getuserbankcards() {
        params.clear();
        params.put("token", getToken());
        getDate(params, Constants.WITHDRAWBANKQUERY, handler, GETBANKSUCCES, GETBANKFAIL, false,
            true, false);
    }

    /**
     * 下载银行图标， towhich表示走两个不同的路线
     * @Description: TODO(这里描述这个类的作用) 
     * @Author pc
     * @Date 2015年9月1日 上午10:11:10
     */
    class downBankTask extends AsyncTask<String, Integer, Boolean> {

        private Bitmap bankLogo;
        private CashBank mCashBank;
        private boolean toWhich;

        public downBankTask(CashBank bli, boolean which) {
            this.mCashBank = bli;
            this.toWhich = which;
        }

        @Override
        protected Boolean doInBackground(String... params) {
            bankLogo = DownPicUtil.getNetWorkBitmap(params[0]);
            return true;
        }

        // 下载完成回调
        @Override
        protected void onPostExecute(Boolean result) {
            if (toWhich) {
                bankcard_icon.setBackgroundDrawable(new BitmapDrawable(bankLogo));
            } else {
                PromptManager.showConfirmAlert4(CashPage.this, false, bankLogo,
                    mCashBank.getName(), StringUtils.hidecardMidString(mCashBank.getCardNo()),
                    new ReChargeClickListener() {

                        @Override
                        public void onClickConfirm() {
                            check_tag = CHECKBYQUICKBANK;
                            bindcard();
                        }

                        @Override
                        public void onClickCancel() {
                            onBackPress();
                        }
                    });
            }
            super.onPostExecute(result);
        }

        // 更新进度回调
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

    }

    private void initPage() {

        List<CashBank> cashBankList = JSON.parseArray(mWithdrawBank.getBankList(), CashBank.class);
        application.setCashBanks(cashBankList);

        if (ListUtils.isEmpty(cashBankList)) { //集合为空说明是新用户，需要提示用户绑卡
            PromptManager.showConfirmAlert4(CashPage.this, true, null, null, null,
                new ReChargeClickListener() {

                    @Override
                    public void onClickConfirm() {
                        check_tag = CHECKBYNOTQUICKBANK;
                        bindcard();
                    }

                    @Override
                    public void onClickCancel() {
                        onBackPress();
                    }
                });
        } else {
            boolean isQuick = false;
            for (int i = 0; i < cashBankList.size(); i++) {
                if ("false".equals(cashBankList.get(i).getIsView())) { //有一张卡isview为false，说明这张卡是快捷卡需要绑定(连连卡)
                    isQuick = true;
                    new downBankTask(cashBankList.get(i), false).execute(Constants.HOST
                                                                         + cashBankList.get(i)
                                                                             .getBankLogo());
                    break;
                }
            }
            if (!isQuick) {
                application.getcashBanks().get(0).setCashChecked(true);
                setCashBankCard(cashBankList.get(0));
            }
        }

    }

    public void setCashInfo() {
        //        String cash = ArithUtils.sub(userAsset.avaliable + "", withDraw.getFee() + "");
        //        if (Double.parseDouble(cash) <= 0) {
        //            cash = "0";
        //        }
        money_avaliable.setText(ArithUtils.coverMoneyComma(userAsset.avaliable + ""));
    }

    /**
     * 填充银行卡的信息
     * 
     * @param cashBanklistItem
     */
    private void setCashBankCard(CashBank cashBanklistItem) {
        cashBankCard = cashBanklistItem;
        bankcard_layout.setEnabled(false);
        //判断是否可以添加银行卡
        if ("true".equals(mWithdrawBank.getAllowAddCard())) {
            more_bankcard.setVisibility(View.VISIBLE);
            bankcard_layout.setEnabled(true);
        }
        if ("true".equals(cashBanklistItem.getIsHFQuickCard())) {
            tv_unbind_bank.setVisibility(View.VISIBLE);
        } else if ("false".equals(cashBanklistItem.getIsHFQuickCard())) {
            tv_unbind_bank.setVisibility(View.GONE);
        }

        bankcard_no.setText(StringUtils.hidecardMidString(cashBankCard.cardNo));
        new downBankTask(cashBankCard, true).execute(Constants.HOST + cashBankCard.getBankLogo());
        bank_detail_text.setText(cashBankCard.name);

        handleCashRule(cashBanklistItem);
    }

    /**
     * 不同提现方式的逻辑处理
     * 
     * @param cashBanklistItem
     */
    private void handleCashRule(CashBank cashBanklistItem) {
        tv_cash_timely_des.setText(cashBanklistItem.getImmediateCashMsg());
        tv_cash_common_des.setText(cashBanklistItem.getGenneralCashMsg());
        if ("1".equals(cashBanklistItem.getIsImmediateCash())) {
            cash_layout_timely.setBackgroundColor(Color.parseColor("#fef9f3"));
            cb_cash_timely.setChecked(true);
            iv_cash_timely.setBackgroundResource(R.drawable.cash_immediate_can);
            tv_cash_timely.setTextColor(getResources().getColor(color.gray_2));
            tv_cash_timely_des.setTextColor(getResources().getColor(color.gray_2));

            cash_layout_common.setBackgroundColor(getResources().getColor(color.white));
            cb_cash_common.setChecked(false);
            cashType = CASHTYPETIMELY;
        } else if ("0".equals(cashBanklistItem.getIsImmediateCash())) {
            iv_cash_timely.setBackgroundResource(R.drawable.cash_immediate_canot);
            tv_cash_timely.setTextColor(getResources().getColor(color.gray_1));
            tv_cash_timely_des.setTextColor(getResources().getColor(color.gray_1));
            cash_layout_timely.setBackgroundColor(getResources().getColor(color.white));
            cb_cash_timely.setChecked(false);
            if ("1".equals(cashBanklistItem.getIsGeneralCash())) {
                cash_layout_common.setBackgroundColor(Color.parseColor("#fef9f3"));
                cb_cash_common.setChecked(true);
                iv_cash_common.setBackgroundResource(R.drawable.cash_general_can);
                tv_cash_common.setTextColor(getResources().getColor(color.gray_2));
                tv_cash_common_des.setTextColor(getResources().getColor(color.gray_2));
                cashType = CASHTYPECOMMON;
            } else if ("0".equals(cashBanklistItem.getIsGeneralCash())) {
                iv_cash_common.setBackgroundResource(R.drawable.cash_general_canot);
                tv_cash_common.setTextColor(getResources().getColor(color.gray_1));
                tv_cash_common_des.setTextColor(getResources().getColor(color.gray_1));
                cash_layout_common.setBackgroundColor(getResources().getColor(color.white));
                cb_cash_common.setChecked(false);
            }
        }
        if (Integer.parseInt(cashBanklistItem.getFreeCashCount()) > 0) { //免费则不需要请求预提现接口，。手续费为0
            ll_mianfei.setVisibility(View.VISIBLE);
            tv_yuan.setVisibility(View.GONE);
            tv_charge_fee.setText("免费");
            tv_count.setText(cashBanklistItem.getFreeCashCount());
            isAskWithDrawFee = false;
        } else { //没有免费次数了，需要请求预提现接口来看手续费是多少
            ll_mianfei.setVisibility(View.GONE);
            tv_yuan.setVisibility(View.VISIBLE);
            tv_charge_fee.setText("--");
            isAskWithDrawFee = true;
        }

    }

    @Override
    protected void onBackPress() {
        LogUtil.info(CashPage.class, "onBackPress");
        hideSoftInputMethod(mContext, cash_amount);
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    protected void toCashTips() {
        ConstantDataUnit czsm = getConstantDataByKey(Constants.dz_txsm);
        if(czsm != null){
            Intent intent = new Intent(mContext, WebViewPage.class);
            intent.putExtra("url", czsm.getVal());
            intent.putExtra("title", "提现");
            startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
        }
    }
}
